package com.zjw.zy.coreui.fragment

import android.graphics.drawable.BitmapDrawable
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import androidx.fragment.app.activityViewModels
import com.zjw.zy.R
import com.zjw.zy.base.BaseFragment
import com.zjw.zy.ext.go
import com.zjw.zy.manager.PageManager
import com.zjw.zy.setting.SettingStorage
import com.zjw.zy.utils.*
import com.zjw.zy.utils.taskpool.TaskPool
import com.zjw.zy.viewmodel.SettingViewModel

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/9/26 9:25
 */
class StartUpFragment : BaseFragment() {


    private val settingViewModel: SettingViewModel by activityViewModels()


    private val image: ImageView by lazy { findViewById(R.id.start_up_image) }

    override fun onViewRestore(savedInstanceState: Bundle?) {}

    override fun onSubscribe() {
        settingViewModel.getStartUpImage().observe(this) {
            it?.let {
                PathUtils.getBitmapFromPath(it.value)?.apply {
                    image.background = BitmapDrawable(resources, this)
                }
            } ?: kotlin.run {
                //走默认图标
                image.background = ResourcesUtils.getDrawable(R.drawable.love_girl_yy_1)
            }
        }

    }

    override fun onBindViewListener() {}
    override fun onBindViewValue() {
    }

    override fun onFindView(rootView: View) {}

    override fun onLayoutId(): Int {
        return R.layout.start_up
    }

    override fun onPageExit() {
    }

    override fun onPageEnter() {

        WindowUtils.fullMode()

        //请求权限
        AppUtils.install {
            PermissionUtils.requestPermissionWhenLaunch()
            SettingStorage.resetOption()
        }

        //检查是否得到授权
        if (!AppUtils.isAccess()) {
            TaskPool.main().postDelayed({ go(R.id.nav_access_fragment) }, 2000L)
            return
        }

        TaskPool.main().postDelayed({ PageManager.goMain() }, 2000L)
    }

}